package com.syt.user.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.syt.client.cmn.DictFeignClient;
import com.syt.enums.DictEnum;
import com.syt.model.user.Patient;
import com.syt.user.mapper.PatientMapper;
import com.syt.user.service.PatientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 就诊人表 服务实现类
 * </p>
 *
 * @author xx
 * @since 2023-02-20
 */
@Service
public class PatientServiceImpl extends ServiceImpl<PatientMapper, Patient> implements PatientService {

    @Autowired
    private DictFeignClient dictFeignClient;


    /**
     * 删除就诊人
     * @param id
     */
    @Transactional
    @Override
    public void removePatientById(Long id) {
        baseMapper.deleteById(id);
    }

    /**
     * 修改就诊人
     * @param patient
     */
    @Transactional
    @Override
    public void updatePatient(Patient patient) {
       baseMapper.updateById(patient);
    }

    /**
     * 根据id获取就诊人信息
     * @param id
     * @return
     */
    @Override
    public Patient getPatientById(Long id) {
        return this.packPatient(baseMapper.selectById(id));
    }

    /**
     * 添加就诊人
     * @param patient
     */
    @Transactional
    @Override
    public void addPatient(Patient patient) {
       baseMapper.insert(patient);
    }

    /**
     * 获取就诊人列表 根据就诊人id查找就诊人列表
     * @param userId
     * @return
     */
    @Override
    public List<Patient> findPatientAllByUserId(Long userId) {
        QueryWrapper<Patient> wrapper=new QueryWrapper<>();
        wrapper.eq("user_id", userId);
        List<Patient> patientList = baseMapper.selectList(wrapper);
        //远程调用得到编码对应具体的内容，查询数据字典表内容
        patientList.stream().forEach(item -> {
            //封装其他参数的值
           this.packPatient(item);
        });

        return patientList;
    }

    //封装其他参数的值
    private Patient packPatient(Patient patient) {
        //根据证件类型编码，获取证件类型具体指
        String certificatesTypeString = dictFeignClient.getName(DictEnum.CERTIFICATES_TYPE.getDictCode(), patient.getCertificatesType());//联系人证件
        //联系人证件类型
        String contactsCertificatesTypeString = dictFeignClient.getName(DictEnum.CERTIFICATES_TYPE.getDictCode(), patient.getContactsCertificatesType());
        //省
        String provinceString = dictFeignClient.getName(patient.getProvinceCode());
        //市
        String cityString = dictFeignClient.getName(patient.getCityCode());
        //区
        String districtString = dictFeignClient.getName(patient.getDistrictCode());
        patient.getParam().put("certificatesTypeString", certificatesTypeString);
        patient.getParam().put("contactsCertificatesTypeString", contactsCertificatesTypeString);
        patient.getParam().put("provinceString", provinceString);
        patient.getParam().put("cityString", cityString);
        patient.getParam().put("districtString", districtString);
        patient.getParam().put("fullAddress", provinceString + cityString + districtString + patient.getAddress());
        return patient;
    }
}
